const api = require('./api.js');
//结束任务游戏或游戏挑战失败统计 ModuleId（课程ID） SubtitleId（1、单词 2、游戏 3、任务） Mark（游戏几，任务几） Status（1、结束 2、失败）
function statistics (OpenId,ModuleId,SubtitleId,Mark,Status) {
    var dataDic = {
        OpenId: OpenId,
        ModuleId: ModuleId,
        SubtitleId: SubtitleId,
        Mark: Mark,
        Status: Status
    }
    api.methodBasicBasicEnglish_CountStatistics({
        dataDic: dataDic,
        success: function (res) {
            console.log('统计成功', res)
        },
        fail: function (err) {
            console.log('统计失败', err)
        }
    })
}
//调用扇贝音频播放
function Sbaudio(word){
    wx.request({
        url: 'https://api.shanbay.com/bdc/search/',
        data: {
          word: word
        },
        success: function (res) {
          console.log(res, '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
          innerAudioContext.src = res.data.data.audio_addresses.uk[0]
          innerAudioContext.play()
          innerAudioContext.onEnded((res) => {
            that.stopMusic = true
  
          })
        }
      })
}
//绘制倒计时效果
function countDown(params, RemainingTime, scale, cb) {
  console.log('播放倒计时效果');
  let ctx_gray = wx.createCanvasContext('canvas-gray');

  let radius = 25

  //保证小屏幕能够完全显示出来
  if (scale < 0.9) {
    radius = 19
  }

  //画灰色的圆弧
  ctx_gray.setLineWidth(10 * scale);
  ctx_gray.arc(33 * scale, 35 * scale, radius * scale, -0.5 * Math.PI, 1.5 * Math.PI)
  ctx_gray.setStrokeStyle('#dcdcdc');
  ctx_gray.stroke();
  ctx_gray.draw(true);

  let ctx_red = wx.createCanvasContext('canvas-red');
  let time = RemainingTime * 1000;
  let space = 50; //时间间隔，毫秒
  // let step = 2 * Math.PI * space * 0.32 / time; //移动步长
  // let bili = 20 * time / 1000;
  let step = space * 2 * Math.PI / time;
  let totalTime = 0; //花费的总时间
  let totalStep = 0; //走过的总步长

  let timer_daojishi = setInterval(function () {
    totalStep += step;
    totalTime += space;
    ctx_red.setLineWidth(10 * scale);
    ctx_red.setLineCap('round');
    drawcircle(ctx_red, totalStep, radius);
    if (totalTime >= time) {
      clearInterval(timer_daojishi);
      typeof cb == 'function' && cb();
      //倒计时结束清除画布
      ctx_gray.clearRect(0, 0, 140, 140);
      ctx_gray.draw(true);
      ctx_red.clearRect(0, 0, 140, 140);
      ctx_red.draw(true);
      return;
    }
  }, space)

  //画弧函数
  function drawcircle(ctx, step, radius) {
    ctx.arc(33 * scale, 35 * scale, radius * scale, -0.5 * Math.PI, -0.5 * Math.PI + step);
    ctx.setStrokeStyle('#3DBF8F');
    ctx.stroke();
    ctx.draw(true);
  }
}
//检查用户录音授权 已经授权返回true 未授权会提示用户授权并返回授权结果
function checkRecord(cb) {
    let _self = this
    wx.getSetting({
        success: (res) => {
            console.log(res)
            if (res.authSetting['scope.record'] != true) {
                wx.authorize({
                    scope: 'scope.record',
                    success: (res) => {
                        typeof cb == "function" && cb(true)
                    },
                    fail: () => {
                        wx.showModal({
                            title: '温馨提示',
                            content: '您暂未授权使用录音功能，这可能会影响部分功能使用！',
                            showCancel: false,
                            cancelText: '确定',
                            confirmText: '去授权',
                            success: (res) => {
                                wx.openSetting({})
                            }
                        })
                    }
                })
            } else {
                typeof cb == "function" && cb(true)
            }
        },
        fail: (res) => {
            typeof cb == "function" && cb(false)
        }
    })
}
module.exports = {
  countDown,
  checkRecord,
  statistics
}